import {
  getOrderRecord,
  createQrcode,
  loginWithCode,
  beShared
} from "../../../api.js"
var app = getApp().globalData
Page({
  data: {
    featureTab: [ {
        icon: "https://document.growthupup.com/consignment1.png",
        to: "/pages/consignment/consignmentList/consignmentList",
        text: "寄卖管理"
      }, {
        icon: "https://document.growthupup.com/personalDiscount.png",
        to: "/pages/myList/coupon/coupon",
        text: "优惠券"
      },
      {
        icon: "https://document.growthupup.com/personalCollection.png",
        to: "/pages/myList/wishlist/wishlist",
        text: "收藏"
      },
      {
        icon: "https://document.growthupup.com/personalNew.png",
        to: "/pages/myList/message/message?type=3",
        text: "客服"
      }, 
      {
        icon: "https://document.growthupup.com/Consignment3.png",
        to: "/pages/myList/bankCard/bankCard",
        text: "银行卡管理"
      },
      {
        icon: "https://document.growthupup.com/personalAdd.png",
        to: "/pages/myList/addList/addList?stateTyp=3",
        text: "地址管理"
      },
      {
        icon: "https://document.growthupup.com/personalQuestion.png",
        to: "/pages/myList/questionList/questionList",
        text: "常见问题"
      },
      {
        icon: "https://document.growthupup.com/personalFee.png",
        to: "/pages/myList/feedback/feedback",
        text: "意见反馈"
      }
    ],
    shar_bj: '',
    alert_none: false,
    alert_erwei: '',
    alert_upType: 0,
    user_id: ''
  },
  onShow() {
    getApp().startpage(new Date().getTime())
    let user = getApp().globalData.Store.user
    this.setData({
      user
    })
  },
  onLoad() {
    let that = this;
    let user = getApp().globalData.Store.user
    this.setData({
      user
    })
  },



  // 分享画布
  canvasImg() {
    const ctx = wx.createCanvasContext('myCanvas');
    const img = this.data.shar_bj;
    const name = this.data.user.name;
    const erwei = this.data.alert_erwei
    const topimageurl = this.data.topimageurl;
    ctx.save();
    ctx.beginPath();
    ctx.drawImage(img, 0, 0, 269, 406); //里面的参数无非就是图片放置的位置即图片的横纵坐标，图片的宽高
    ctx.draw(true);
    ctx.drawImage(erwei, 205, 345, 50, 50);
    ctx.setFillStyle("#fff");
    ctx.setFontSize(10); //字大小
    ctx.setTextAlign('left'); //是否居中显示，参考点画布中线
    ctx.fillText('芮享一种轻奢生活的新方式', 65, 385); //150:canvas画布宽300，取1/2，中间，280：纵向位置
    ctx.setFontSize(8);
    ctx.setTextAlign('left');
    ctx.fillText(name, 65, 365);
    ctx.draw(true);
    ctx.arc(42 / 2 + 13, 42 / 2 + 350, 42 / 2, 0, Math.PI * 2, false);
    ctx.clip(); //画好了圆 剪切  原始画布中剪切任意形状和尺寸。一旦剪切了某个区域，则所有之后的绘图都会被限制在被剪切的区域内 这也是我们要save上下文的原因
    ctx.drawImage(topimageurl, 13, 350, 46, 46); // 推进去图片，必须是https图片    
    ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 还可以继续绘制
    ctx.draw(true);
    ctx.draw(true);
  },
  saveImg() {
    var that = this
    wx.canvasToTempFilePath({
      canvasId: 'myCanvas',
      success: function(res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function(res) {
            wx.showToast({
              title: '保存成功',
            })
            that.setData({
              alert_none: false,
            })
          },
          fail: function(res) {}
        })
      }
    })
  },
  // 定义分享
  onShareAppMessage(res) {
    return {
      title: "芮享一种轻奢生活的新方式",
      imageUrl: "https://document.growthupup.com/shareday.png?t=" + new Date().getTime(),
      path: '/pages/tabBar/index/index?user_id=' + getApp().globalData.Store.user.id,
      success: (res) => {
        this.setData({
          alert_none: false
        })
      },
    }
  },
  fenxiang(){
    beShared({
      token: app.Store.token,
      source: 2,
      user_id: app.Store.user.id
    }).then(res => {
      console.log(res)
    })

  },

  alert_share_btn() {
    var that = this
    createQrcode(getApp().globalData.Store.token).then(res => {
      if (res.data.code == 666) {
        that.setData({
          alert_erwei: res.data.data.key
        })
        if (that.data.alert_upType == 0) {
          wx.showLoading({
            title: '正在生成分享图片',
          })
          wx.downloadFile({
            url: 'https://document.growthupup.com/alert_img_bjj_day.png',
            success: function(res) {
              that.setData({
                shar_bj: res.tempFilePath
              })
              wx.downloadFile({
                url: getApp().globalData.Store.user.topimageurl,
                success: function(res) {
                  that.setData({
                    topimageurl: res.tempFilePath,
                  })
                  wx.downloadFile({
                    url: that.data.alert_erwei,
                    success: function(res) {
                      wx.hideLoading()
                      that.setData({
                        alert_erwei: res.tempFilePath,
                        alert_none: true,
                        alert_upType: 1
                      })
                      that.canvasImg()
                    }
                  })
                }
              })
            }
          })
        } else {
          wx.downloadFile({
            url: that.data.alert_erwei,
            success: function(res) {
              wx.hideLoading()
              that.setData({
                alert_erwei: res.tempFilePath,
                alert_none: true,
              })
              that.canvasImg()
            }
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  alert_off() {
    this.setData({
      alert_none: false
    })
  },
  onHide() {
    getApp().endpage(new Date().getTime())
    this.setData({
      alert_none: false
    })
  },
  // 检查用户信息授权
  checkUserInfo() {
    const xp = this;
    wx.getSetting({
      success: function(res) {
        if (!res.authSetting['scope.userInfo']) {
          xp.setData({
            isAuth: false
          })
          wx.navigateTo({
            url: '/pages/public/shouquan/shouquan',
          })
        } else {
          xp.setData({
            isAuth: true
          })
          wx.getUserInfo({
            success(res) {
              xp.loginWithCode(res.userInfo)
            }
          })
        }
      }
    })
  },
  //登陆
  loginWithCode(userInfo) {
    const xp = this
    wx.login({
      success({
        code
      }) {
        loginWithCode({
          ...userInfo,
          code,
          business_id: getApp().globalData.businessId
        }).then(res => {
          if (res.data.code === 666) {
            getApp().globalData.Store = res.data.data
            xp.getOrderRecord()
          }
        })
      }
    })
  },
  navlitt() {
    if (this.data.user.phone) {
      wx.navigateTo({
        url: '/pages/consignment/newConsignment/newConsignment',
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请先绑定手机，在进行寄卖',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/public/bindphone/bindphone',
            })
          } else if (res.cancel) {
            // console.log('用户点击取消')
          }
        }
      })
    }
  },
})